﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RJ.Objects.Common;
using RJ.Objects.GameEngine;
using RJ.Objects.PokerCards;
using RJ.Euchre.App.Views;
using System.Drawing;

namespace RJ.Euchre.App.Game
{
    public class EuchrePlayer : GamePlayer
    {
        private EuchrePlayingState playingState;
        public EuchrePlayingState PlayingState
        {
            get { return playingState; }
            set { playingState = value; }
        }

        private int player;
        public int Player { get { return player; } }

        private Point statusStringLocation;

        private Hand playingHand;
        public Hand PlayingHand
        {
            get { return playingHand; }
            set { playingHand = value; }
        }

        public EuchreTableView GameView
        {
            get { return (game as EuchreGameState).gameView as EuchreTableView; }
        }

        public EuchreDeck DeckToDeal
        {
            get { return GameView.deck; }
        }

        private Font playerFont;
        private Brush playerBrush;

        public event NotifyTurn TurnFinishedNotification;

        public EuchrePlayer(GameState game, PlayerType type, Sprite sprite, int player)
            : this(game, type, AI.Dumb, sprite, player) { }

        public EuchrePlayer(GameState game, PlayerType type, AI ai, Sprite sprite, int player)
            : base(game, type, ai, sprite)
        {
            this.player = player;

            var euchreSettings = game.settings as EuchreSettings;
            if (euchreSettings != null)
            {
                var pq = euchreSettings.QuadrantModifiers[player - 1];
                Point ssOffset = new Point(120, 200);
                Point qo = GetQuadrantOffset();
                statusStringLocation = new Point(
                    (game.GameCenter.Width) + ((ssOffset.X + qo.X) * pq.X),
                    (game.GameCenter.Height) + ((ssOffset.Y + qo.Y) * pq.Y)
                );
            }

            playerFont = new Font("Arial", 24);
            playerBrush = new SolidBrush(Color.Firebrick);
        }

        private Point GetQuadrantOffset()
        {
            Point offset = Point.Empty;
            if (player % 2 != 0)
            {
                offset.X = 125;
            }

            if (player > 2)
            {
                offset.Y = 20;
            }

            return offset;
        }

        public override void Update(double gameTime, double elapsedTime)
        {
            Console.WriteLine("Player {0} is updating. PlayingState: {1}", player, playingState);

            switch (playingState)
            {
                case EuchrePlayingState.Dealing:
                    switch (PlayerType)
                    {
                        case Objects.GameEngine.PlayerType.Computer:
                            break;
                        case Objects.GameEngine.PlayerType.Human:
                            break;
                    }
                    break;
                case EuchrePlayingState.Playing:
                    break;
                default:
                    break;
            }
        }

        public override void Draw(System.Drawing.Graphics g)
        {
            switch (playingState)
            {
                case EuchrePlayingState.Dealing:
                    g.DrawString("Dealing", playerFont, playerBrush, statusStringLocation.ToPointF());
                    break;
                default:
                    break;
            }

            base.Draw(g);
        }

        private void OnTurnFinishedNotification()
        {
            if (TurnFinishedNotification != null)
                TurnFinishedNotification(this, new EuchreGameEventArgs(game, EventState.NotifyTurn, EuchrePlayingState.Done, player));
        }

        public void TurnNotification(object sender, GameEventArgs gameEventArgs)
        {
            var euchreGameArgs = gameEventArgs as EuchreGameEventArgs;
            if (euchreGameArgs != null && euchreGameArgs.state == EventState.NotifyTurn)
            {
                if (player == euchreGameArgs.player)
                {
                    playingState = euchreGameArgs.euchrePlayingState;
                }
                else
                    playingState = EuchrePlayingState.Waiting;
            }
        }
    }

    public enum EuchrePlayingState
    {
        Playing,
        Dealing,
        Waiting,
        Done
    }
}
